// pages/sign/sign.js
const app = getApp()
const util = require('../../utils/util')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        signBegin:'',
        signEnd:'',

        userId:'',
        signId:'',
        localdate:'',
        signaddress:'',

        issign:false,

        ismix:false,

        isqr:false,

        ismap:false,
        lng: 0,
        lat: 0,
        markers:[],
        circles:[],

        isCamera:false,
        isshowpic:false,
        isUseCamera: false,
        face: '',

        toast: false,
        hideToast: false,
        warnToast: false,
        hideWarnToast: false,
        message:''
    },

    mixscanCode:function () {
        var date = util.formatTime(new Date());
        console.log(date);
        this.setData({
            userId:app.globalData.userId,
            localdate:date
        })
        if(date<this.data.signBegin || date>this.data.signEnd){
            this.setData({
                warnToast: true,
                message:'未在签到时间内'
            });
            setTimeout(() => {
                this.setData({
                    hidewarnToast: true,
                });
                setTimeout(() => {
                    this.setData({
                        warnToast: false,
                        hidewarnToast: false,
                    });
                }, 300);
            }, 3000);
        }else{
            var that = this;
            wx.scanCode({ //扫描API
                onlyFromCamera: true,
                scanType: ['barCode', 'qrCode'],
                success(res) { //扫描成功
                    console.log(res);
                    var userId = that.data.userId;
                    var signId = that.data.signId;
                    var signTime = that.data.localdate;
                    var scanCodeMsg = res.result+'?userId='+userId+'&signId='+signId+'&signTime='+signTime;
                    console.log(scanCodeMsg);
                    wx.reLaunch({
                        url:scanCodeMsg,
                    })
                }
            })
        }
    },

    scanCode: function () {
        var date = util.formatTime(new Date());
        console.log(date);
        this.setData({
            userId:app.globalData.userId,
            localdate:date
        })
        if(date<this.data.signBegin || date>this.data.signEnd){
            this.setData({
                warnToast: true,
                message:'未在签到时间内'
            });
            setTimeout(() => {
                this.setData({
                    hidewarnToast: true,
                });
                setTimeout(() => {
                    this.setData({
                        warnToast: false,
                        hidewarnToast: false,
                    });
                }, 300);
            }, 3000);
        }else{
            var that = this;
            wx.scanCode({ //扫描API
                onlyFromCamera: true,
                scanType: ['barCode', 'qrCode'],
                success(res) { //扫描成功
                    console.log(res);
                    var userId = that.data.userId;
                    var signId = that.data.signId;
                    var signTime = that.data.localdate;
                    var scanCodeMsg = res.result+'?userId='+userId+'&signId='+signId+'&signTime='+signTime; //获取二维码的路径信息
                    console.log(scanCodeMsg,userId,signId,signTime) //输出二维码信息
                    wx.request({
                        url: scanCodeMsg,
                        header: {'content-type':'application/json'},
                        method: "GET",
                        success: ({data}) => {
                            console.log(data);
                            console.log(data.status);
                            if(data.status === 'success'){
                                this.setData({
                                    toast: true
                                });
                                setTimeout(() => {
                                    this.setData({
                                        hideToast: true
                                    });
                                    setTimeout(() => {
                                        this.setData({
                                            toast: false,
                                            hideToast: false,
                                        });
                                    }, 300);
                                }, 3000);
                            }else{
                                this.setData({
                                    warnToast: true,
                                    message:data.message
                                });
                                setTimeout(() => {
                                    this.setData({
                                        hidewarnToast: true,
                                    });
                                    setTimeout(() => {
                                        this.setData({
                                            warnToast: false,
                                            hidewarnToast: false,
                                        });
                                    }, 300);
                                }, 3000);
                                console.log(data.message);
                            }
                        },
                        fail:(err)=> {
                            this.setData({
                                warnToast: true,
                                message:'未在签到时间内'
                            });
                            setTimeout(() => {
                                this.setData({
                                    hidewarnToast: true,
                                });
                                setTimeout(() => {
                                    this.setData({
                                        warnToast: false,
                                        hidewarnToast: false,
                                    });
                                }, 300);
                            }, 3000);
                            console.log(data.message);
                        }
                    })
                }
            })
        }    
    }, 

    drawCircle(center) {
        var cirle = {
            latitude :center.latitude || center.lat || center[0],
            longitude :center.longitude || center.lng || center[1],
            radius:100,
            strokeWidth:1,
            strokeColor:'#FF00FF'
        };
        this.setData({
            circles:[cirle]
        })
    },
    sign(){
        var date = util.formatTime(new Date());
        console.log(date);
        this.setData({
            userId:app.globalData.userId,
            localdate:date
        })
        if(date<this.data.signBegin || date>this.data.signEnd){
            this.setData({
                warnToast: true,
                message:'未在签到时间内'
            });
            setTimeout(() => {
                this.setData({
                    hidewarnToast: true,
                });
                setTimeout(() => {
                    this.setData({
                        warnToast: false,
                        hidewarnToast: false,
                    });
                }, 300);
            }, 3000);
        }else{
            let{userId,signId,localdate,signaddress} = this.data;
            let postData = {
                user_Id:this.data.userId,
                sign_Id:this.data.signId,
                sign_Time:this.data.localdate,
                address:this.data.signaddress
            }
            postData = {...this.data}
            postData = {userId,signId,localdate,signaddress}
            console.log(postData);
            wx.request({
                url: 'http://localhost/public/sign/sign/mapsign',
                method: "POST",
                data: postData,
                success: ({data}) => {
                    console.log(data);
                    console.log(data.status);
                    
                        if(data.status === 'success'){
                            this.setData({
                                toast: true
                            });
                            setTimeout(() => {
                                this.setData({
                                    hideToast: true
                                });
                                setTimeout(() => {
                                    this.setData({
                                        toast: false,
                                        hideToast: false,
                                    });
                                }, 300);
                            }, 3000);
                        }else{
                            this.setData({
                                warnToast: true,
                                message:data.message
                            });
                            setTimeout(() => {
                                this.setData({
                                    hidewarnToast: true,
                                });
                                setTimeout(() => {
                                    this.setData({
                                        warnToast: false,
                                        hidewarnToast: false,
                                    });
                                }, 300);
                            }, 3000);
                            console.log(data.message);
                        }
                    
                },
                fail:(err)=> {
                    this.setData({
                        warnToast: true,
                        message:'签到错误'
                    });
                    setTimeout(() => {
                        this.setData({
                            hidewarnToast: true,
                        });
                        setTimeout(() => {
                            this.setData({
                                warnToast: false,
                                hidewarnToast: false,
                            });
                        }, 300);
                    }, 3000);
                    console.log(data.message);
                }
            })
        } 
    },

    showCamera() {
        this.setData({
            isshowpic:false,
            face:'',
            isUseCamera: true
        })
    },
    takePhoto() {
        this.setData({
            isUseCamera: true
        })
        // 得到相机上下文。可以想像成为这个contex就是相机的按键面板
        const ctx = wx.createCameraContext();
        // 在按键面板上按了下快门键
        ctx.takePhoto({
            complete: (data) => {
                console.log(data)
                // 用条件渲染拿掉相机
                this.setData({
                    isshowpic:true,
                    isUseCamera: false
                });
            // 将图片的地址设置为本次拍摄的图片的地址
                this.setData({
                    face: data.tempImagePath
                });
            }
        })
    },
    uploadFace() {
        var date = util.formatTime(new Date());
        console.log(date);
        this.setData({
            userId:app.globalData.userId,
            localdate:date
        })
        if(date<this.data.signBegin || date>this.data.signEnd){
            this.setData({
                warnToast: true,
                message:'未在签到时间内'
            });
            setTimeout(() => {
                this.setData({
                    hidewarnToast: true,
                });
                setTimeout(() => {
                    this.setData({
                        warnToast: false,
                        hidewarnToast: false,
                    });
                }, 300);
            }, 3000);
        }else{
            wx.uploadFile({
    
                url: 'http://localhost/public/sign/sign/photosign',
                filePath: this.data.face,
                name: 'file',
                formData: {
                    'user_Id':this.data.userId,
                    'sign_Id':this.data.signId,
                    'sign_Time':this.data.localdate,
                    'user': 'test'
                },
                success: ({data}) => {
                    console.log(data);
                    console.log(data.status);
                    
                        if(data.status === 'success'){
                            this.setData({
                                toast: true
                            });
                            setTimeout(() => {
                                this.setData({
                                    hideToast: true
                                });
                                setTimeout(() => {
                                    this.setData({
                                        toast: false,
                                        hideToast: false,
                                    });
                                }, 300);
                            }, 3000);
                        }else{
                            this.setData({
                                warnToast: true,
                                message:data.message
                            });
                            setTimeout(() => {
                                this.setData({
                                    hidewarnToast: true,
                                });
                                setTimeout(() => {
                                    this.setData({
                                        warnToast: false,
                                        hidewarnToast: false,
                                    });
                                }, 300);
                            }, 3000);
                            console.log(data.message);
                        }
                    
                },
                fail:(err)=> {
                    this.setData({
                        warnToast: true,
                        message:'未在签到时间内'
                    });
                    setTimeout(() => {
                        this.setData({
                            hidewarnToast: true,
                        });
                        setTimeout(() => {
                            this.setData({
                                warnToast: false,
                                hidewarnToast: false,
                            });
                        }, 300);
                    }, 3000);
                    console.log(data.message);
                }
            })
        }    
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //复合签到
        console.log(app.globalData.userId);
        if(options.type==0){
            wx.request({
                url: 'http://localhost/public/sign/sign/ismixsign',
                data: {
                    userId:app.globalData.userId,
                    signId:options.Id
                },
                header: {'content-type':'application/json'},
                method: "GET",
                success: ({data}) => {
                    console.log(data)
                    this.setData({
                        issign:data.status,
                    })
                },
            })
            this.setData({
                ismix:true,
            })
        }
        //二维码签到
        if(options.type==1){
            wx.request({
                url: 'http://localhost/public/sign/sign/isqrsign',
                data: {
                    userId:app.globalData.userId,
                    signId:options.Id
                },
                header: {'content-type':'application/json'},
                method: "GET",
                success: ({data}) => {
                    console.log(data)
                    this.setData({
                        issign:data.status,
                    })
                },
            })
            this.setData({
                isqr:true,
            })
        }
        //定位签到
        if(options.type==2){
            wx.request({
                url: 'http://localhost/public/sign/sign/ismapsign',
                data: {
                    userId:app.globalData.userId,
                    signId:options.Id
                },
                header: {'content-type':'application/json'},
                method: "GET",
                success: ({data}) => {
                    // console.log(data)
                    this.setData({
                        issign:data.status,
                    })
                },
            })
            // console.log(this.data)
            wx.getLocation({
                type: 'gcj02',
                success:(ret)=>{
                    // console.log(ret)
                    var marker = {
                        id:1,
                        longitude:ret.longitude,
                        latitude:ret.latitude,
                        title: "I am here",
                        // iconPath :"/assets/pin-red.png",
                        width:50,height:50
                    }
                    var longitude = ret.longitude;
                    var latitude = ret.latitude;
                    const key = 'CUABZ-QRALW-ZJCRC-RYLET-E75SE-FLFMJ';
                    wx.request({
                        type: 'gcj02',
                        url:'https://apis.map.qq.com/ws/geocoder/v1',
                        data:{
                            key:key,
                            location:latitude+','+longitude
                        },success:(res)=>{
                            var address = res.data.result.address;
                            var name = res.data.result.formatted_addresses.recommend;
                            // const {recommend} = res.data.result.formatted_addresses
                            // console.log(address,name)
                            this.setData({
                                signaddress:name,
                            })
                        },fail(res){
                            console.log(res)
                        }
                    })
                    this.setData({
                        lng:ret.longitude,
                        lat:ret.latitude,
                        markers:[marker],
                        ismap:true,
                    })
                    this.drawCircle(ret);
                }
            })
        }
        //拍照签到
        if(options.type==3){
            this.setData({
                isCamera:true
            })
            wx.request({
                url: 'http://localhost/public/sign/sign/isphotosign',
                data: {
                    userId:app.globalData.userId,
                    signId:options.Id
                },
                header: {'content-type':'application/json'},
                method: "GET",
                success: ({data}) => {
                    console.log(data)
                    this.setData({
                        issign:data.status,
                    })
                },
            })
        }

        this.setData({
            signId:options.Id,
            signBegin:options.begin,
            signEnd:options.end,
        })
    },

    
})